﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.sharepointutils.application
{
    public class QueryCondition
    {
        public QueryCondition LeftCondition { get; set; }
        public QueryCondition RightCondition { get; set; }
        public ComparisonOperator Operator { get; set; }
        public LogicalJoin Join { get; set; }
        public QueryFieldRef Field { get; set; }
         QueryValue Value { get; set; }

        public QueryCondition(String field, String value, ComparisonOperator comparisonOperator)
        {
            Field = new QueryFieldRef(field);
            Value = new QueryValue(value);
            Operator = comparisonOperator;
        }

        public QueryCondition(QueryCondition leftCondition, QueryCondition rightCondition, LogicalJoin join)
        {
            LeftCondition = leftCondition;
            RightCondition = rightCondition;
            Join = join;
        }

        public override string ToString()
        {
            StringBuilder xml = new StringBuilder();

            if (LeftCondition == null || RightCondition == null)
            {
                xml.Append("<" + Enum.GetName(typeof(ComparisonOperator), this.Operator) + ">");
                xml.Append(this.Field);
                xml.Append(this.Value);
                xml.Append("</" + Enum.GetName(typeof(ComparisonOperator), this.Operator) + ">");
            }
            else
            {
                xml.Append("<" + Enum.GetName(typeof(LogicalJoin), this.Join) + ">");
                xml.Append(this.LeftCondition);
                xml.Append(this.RightCondition);
                xml.Append("</" + Enum.GetName(typeof(LogicalJoin), this.Join) + ">");
            }
            return xml.ToString();
        }

    }
}
